import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_rider/generated/assets.dart';
import 'package:flutter_rider/main/widgets/ext/u_layout.dart';
import 'package:flutter_rider/main/widgets/ext/u_text.dart';
import 'package:flutter_rider/main/widgets/ext/u_title_layout.dart';
import 'package:flutter_rider/main/widgets/state/refreshable_layout.dart';
import 'package:flutter_rider/main/widgets/state/state_layout.dart';
import 'package:flutter_rider/main/widgets/ui_constant.dart';
import 'package:flutter_rider/module/mine/api/msg_api.dart';
import 'package:flutter_rider/module/mine/mine_router.dart';
import 'package:flutter_rider/module/mine/models/msg_record_model.dart';
import 'package:flutter_rider/module/mine/widgets/image_text_widget.dart';
import 'package:flutter_rider/module/mine/widgets/msg_widget.dart';
import 'package:flutter_rider/routers/fluro_navigator.dart';
import 'package:flutter_rider/utils/log.dart';
import 'package:flutter_rider/utils/screen_fit.dart';

class MessagePage extends StatefulWidget {
  const MessagePage({Key? key}) : super(key: key);

  @override
  State<MessagePage> createState() => _MessagePageState();
}

class _MessagePageState extends State<MessagePage> {
  final _stateController = RefreshableLayoutController();
  //请求id默认是0
  var _id = 0;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: UIColor.color_main,
      appBar: UTitleLayout.getBackTitle(context, "消息"),
      body: SafeArea(
          child: Column(
        // crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          _topWidget(),
          _renderList(),
        ],
      )),
    );
  }

  Widget _topWidget() {
    return Container(
      padding: EdgeInsets.only(top: 12.rpx, bottom: 12.rpx),
      color: UIColor.color_content,
      child: Row(
        mainAxisSize: MainAxisSize.max,
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        // //交叉轴的布局方式，对于column来说就是水平方向的布局方式
        // crossAxisAlignment: CrossAxisAlignment.center,
        // //就是子child的垂直布局方向，向上还是向下
        // verticalDirection: VerticalDirection.down,
        children: [
          ImageTextWidget("运单消息", Assets.imageMineMessageText,
                  onTap: _shuntingLogClick, pixel: 24.rpx)
              .expand,
          ImageTextWidget("任务消息", Assets.imageMineMessageTime,
                  onTap: _outsideCarClick, pixel: 24.rpx)
              .expand,
          ImageTextWidget("车辆消息", Assets.imageMineSmsTracking,
                  onTap: _changeLandscapeClick, pixel: 24.rpx)
              .expand,
        ],
      ),
    );
  }

  _shuntingLogClick() {
    Log.i("运单消息");
    NavigatorUtils.push(context, MineRouter.messageDetailPage, arguments: 1);
  }

  _outsideCarClick() {
    Log.i("任务消息");
    NavigatorUtils.push(context, MineRouter.messageDetailPage, arguments: 2);
  }

  _changeLandscapeClick() {
    Log.i("车辆消息");
    NavigatorUtils.push(context, MineRouter.messageDetailPage, arguments: 3);
  }

  Widget _renderList() {
    return Expanded(
      child: Container(
          clipBehavior: Clip.antiAlias,
          margin: EdgeInsets.all(12.rpx),
          decoration: BoxDecoration(
              color: UIColor.color_content,
              borderRadius: BorderRadius.circular(12.rpx)),
          child: RefreshableLayout(
              controller: _stateController,
              urlCallback: _buildUrl,
              handleDataCallback: _handleData,
              singleDataCallback: _singleData,
              itemBuilder: _renderListItem)),
    );
  }

  String _buildUrl() {
    var baseUrl = "${MsgApi.msgRecord}?type=0&id=$_id";
    return baseUrl;
  }

  _handleData(dynamic list) {
    return list.map((e) => MsgRecordModel.fromMap(e)).toList();
  }

  _singleData(dynamic data, bool isRefresh) {
    if (isRefresh) {
      var msg = data as MsgRecordModel;
      _id = msg.id;
    } else {
      _id = 0;
    }
  }

  Widget _renderListItem(dynamic data, int index) {
    return MsgWidget(data as MsgRecordModel);
  }
}
